<?php

class Ddup_Featured_Block_Adminhtml_Products_Edit_Tab_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid
{

    public function __construct()
    {
        parent::__construct();
		$this->setRowClickCallback('categoryProductRowClick');
		$this->setInitRowCallback('categoryProductRowInit');
		$this->setCheckboxCheckCallback('registerCategoryProduct');
        $this->setUseAjax(true);
        $this->setTemplate('featured/widget/grid.phtml');
    }
    
	protected function _addColumnFilterToCollection($column)
    {
        // Set custom filter for in category flag
        if ($column->getId() == 'in_category') {
            $productIds = $this->_getSelectedProducts();
            if (empty($productIds)) {
                $productIds = 0;
            }
            if ($column->getFilter()->getValue()) {
                $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$productIds));
            }
            elseif(!empty($productIds)) {
                $this->getCollection()->addFieldToFilter('entity_id', array('nin'=>$productIds));
            }
        }
        else {
            parent::_addColumnFilterToCollection($column);
        }
        return $this;
    }
    
    public function getProductsGroup()
    {
    	return Mage::registry('products_group_data');
    }
    
    protected function _prepareCollection()
    {
    	if ($this->_getSelectedProducts()) {
    		$this->setDefaultFilter(array('in_category'=>1));
    	}
    	parent::_prepareCollection();
    }
    
	protected function _getSelectedProducts()
    {
        $products = $this->getRequest()->getPost('selected_products');
        if (is_null($products)) {
        	if ($this->getProductsGroup() && $this->getProductsGroup()->getProducts() ) {
    	        $products = $this->getProductsGroup()->getProducts();
     	    	return explode(',', $products);
        	}
        }
        return $products;
    }

    protected function _prepareColumns()
    {
    	$this->addColumn('in_category', array(
    			'header_css_class' => 'a-center',
    			'type'      => 'checkbox',
    			'name'      => 'in_category',
    			'values'	=> $this->_getSelectedProducts(),
    			'align'     => 'center',
    			'index'     => 'entity_id'
    	));
    		
        $this->addColumn('entity_id', array(
                'header'    => Mage::helper('featured')->__('ID'),
                'width'     => '50px',
                'index'     => 'entity_id',
        ));

        $this->addColumn('name', array(
                'header'    => Mage::helper('featured')->__('Name'),
                'index'     => 'name',
        ));

        if ((int)$this->getRequest()->getParam('store', 0)) {
            $this->addColumn('custom_name', array(
                    'header'    => Mage::helper('featured')->__('Name in Store'),
                    'index'     => 'custom_name'
            ));
        }

        $this->addColumn('sku', array(
                'header'    => Mage::helper('featured')->__('SKU'),
                'width'     => '80px',
                'index'     => 'sku'
        ));

        $this->addColumn('price', array(
                'header'    => Mage::helper('featured')->__('Price'),
                'type'      => 'currency',
                'index'     => 'price'
        ));

        $this->addColumn('qty', array(
                'header'    => Mage::helper('featured')->__('Qty'),
                'width'     => '130px',
                'type'      => 'number',
                'index'     => 'qty'
        ));

        $this->addColumn('status', array(
                'header'    => Mage::helper('featured')->__('Status'),
                'width'     => '90px',
                'index'     => 'status',
                'type'      => 'options',
                'source'    => 'catalog/product_status',
                'options'   => Mage::getSingleton('catalog/product_status')->getOptionArray(),
        ));

        /**
         * Check is single store mode
         */
        if (!Mage::app()->isSingleStoreMode()) {
            $this->addColumn('websites',
                array(
                    'header'=> Mage::helper('featured')->__('Websites'),
                    'width' => '100px',
                    'sortable'  => false,
                    'index'     => 'websites',
                    'type'      => 'options',
                    'options'   => Mage::getModel('core/website')->getCollection()->toOptionHash(),
            ));
        }
        
    }
    
	public function getProductsJson()
    {
    	if ($this->getProductsGroup() && $this->getProductsGroup()->getProducts() ) {
	        $products = explode(',', $this->getProductsGroup()->getProducts());
	    	$newProductArr = array();
	    	foreach($products as $_productId){
	    		$newProductArr[$_productId] = '';
	    	}
	        if (!empty($newProductArr)) {
	            return Mage::helper('core')->jsonEncode($newProductArr);
	        }
    	}
    	        
        return '{}';
    }

    public function getGridUrl()
    {
        return $this->getUrl('*/*/productGrid', array('_current'=>true));
    }

    public function getRowUrl($row)
    {
        return $this->getUrl('*/*/jsonProductInfo', array('id' => $row->getId()));
    }

    protected function _prepareMassaction()
    {
        return $this;
    }
}
